package com.jinke.uang.camera;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;

import com.jinke.uang.R;
import com.jinke.uang.base.BaseActivity;
import com.jinke.uang.interfaces.ILoading;
import com.otaliastudios.cameraview.CameraException;
import com.otaliastudios.cameraview.CameraListener;
import com.otaliastudios.cameraview.CameraLogger;
import com.otaliastudios.cameraview.CameraOptions;
import com.otaliastudios.cameraview.CameraView;
import com.otaliastudios.cameraview.PictureResult;
import com.otaliastudios.cameraview.VideoResult;

import androidx.annotation.NonNull;
import crossoverone.statuslib.StatusUtil;

/**
 * @author lixiaonan
 * 功能描述: 其他认证的相关界面
 * 时 间： 2019/4/10 下午7:00
 */
public class CameraPhotoActivity extends BaseActivity implements View.OnClickListener {

    private CameraView camera;

    private long mCaptureTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,
                WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);

        StatusUtil.setUseStatusBarColor(this, Color.parseColor("#00000000"));
        // 第二个参数是是否沉浸,第三个参数是状态栏字体是否为黑色。
        StatusUtil.setSystemStatus(this, true, false);

        setContentView(R.layout.activity_camera_photo);
        CameraLogger.setLogLevel(CameraLogger.LEVEL_VERBOSE);
        camera = findViewById(R.id.camera);
        camera.setLifecycleOwner(this);
        camera.addCameraListener(new CameraListener() {

            @Override
            public void onCameraOpened(@NonNull CameraOptions options) {
            }

            @Override
            public void onCameraError(@NonNull CameraException exception) {
                super.onCameraError(exception);
                message("Got CameraException #" + exception.getReason(), true);
            }

            @Override
            public void onPictureTaken(@NonNull PictureResult result) {
                super.onPictureTaken(result);
                if (camera.isTakingVideo()) {
                    return;
                }
                long callbackTime = System.currentTimeMillis();

                // This can happen if picture was taken with a gesture.
                if (mCaptureTime == 0) {
                    mCaptureTime = callbackTime - 300;
                }

                PicturePreviewActivity.setPictureResult(result);
                Intent intent = new Intent(CameraPhotoActivity.this, PicturePreviewActivity.class);
                intent.putExtra("delay", callbackTime - mCaptureTime);
                startActivityForResult(intent, 0x1234);

                mCaptureTime = 0;

            }

            @Override
            public void onVideoTaken(@NonNull VideoResult result) {
                super.onVideoTaken(result);
            }
        });

        findViewById(R.id.capturePhoto).setOnClickListener(this);
        findViewById(R.id.toggleCamera).setOnClickListener(this);
    }

    @Override
    protected ILoading createLoadingView() {
        return null;
    }

    @Override
    protected int getResId() {
        return 0;
    }

    private void message(String content, boolean important) {
        int length = important ? Toast.LENGTH_LONG : Toast.LENGTH_SHORT;
        Toast.makeText(this, content, length).show();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.capturePhoto:
                //照相
                capturePhoto();
                break;
            case R.id.toggleCamera:
                toggleCamera();
                break;
            default:
                break;
        }
    }


    private void capturePhoto() {
        if (camera.isTakingPicture()) {
            return;
        }
        mCaptureTime = System.currentTimeMillis();

        camera.takePictureSnapshot();
    }


    /**
     * 切换摄像头
     */
    private void toggleCamera() {
        if (camera.isTakingPicture() || camera.isTakingVideo()) {
            return;
        }
        switch (camera.toggleFacing()) {
            case BACK:
                break;
            case FRONT:
                break;
            default:
                break;
        }
    }


    //region Permissions

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        boolean valid = true;
        for (int grantResult : grantResults) {
            valid = valid && grantResult == PackageManager.PERMISSION_GRANTED;
        }
        if (valid && !camera.isOpened()) {
            camera.open();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 0x1234 && resultCode == 0x01) {
            setResult(0x1234, data);
            finish();
        } else if (requestCode == 0x1234 && resultCode == 0x02) {
            camera.takePictureSnapshot();
        }

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        camera.close();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        camera.destroy();
    }
}
